package csu.rjxyg.action.infoAction;

import java.util.ArrayList;
import java.util.List;

import csu.rjxyg.action.BaseAction;
import csu.rjxyg.action.constant.InfoConstant;
import csu.rjxyg.action.constant.UserConstant;
import csu.rjxyg.bean.core.BaseBean;
import csu.rjxyg.bean.core.BaseList;
import csu.rjxyg.bean.core.BaseObject;
import csu.rjxyg.factory.core.UtilFactory;
import csu.rjxyg.service.BaseInfoService;
import csu.rjxyg.service.BaseUserService;
import csu.rjxyg.util.LogUtil;
import csu.rjxyg.util.TimeUtil;

public class RepeatInfoAction extends BaseAction{
	private LogUtil log;	
	private TimeUtil time;
	
	private String[] infoIdList;
	private String[] acceptUserIdList;
	
	public RepeatInfoAction(){
		log = UtilFactory.getLogUtil(this.getClass());
		time = UtilFactory.getTimeUtil();
	}
	
	
	public void repeatInfo(){
		String name = null;
		try{
			BaseObject sendObject = (BaseObject) this.getSession().get(UserConstant.USER_SESSION);
			name = (String) sendObject.get(UserConstant.USER_ID);
			
			List accept = (List) sendObject.get(UserConstant.USER_ACCEPT_INFO);
			List send = (List) sendObject.get(UserConstant.USER_SEND_INFO);
			if( accept == null ){
				accept = new ArrayList();
			}
			if( send == null ){
				send = new ArrayList();
			}
			//检查所要转发的邮件的用户是否持有这些邮件
			for(int i = 0;i < infoIdList.length;i++){
				if( !accept.contains(infoIdList[i]) && !send.contains(infoIdList[i])){
					this.out("error");
					log.info("转发失败，用户:" + name + "未持有:" + infoIdList[i] + "邮件");
					return;
				}
			}
			
			List temp = new ArrayList();
			for(int i = 0; i < infoIdList.length;i++){
				temp.add(infoIdList[i]);
			}
			
			
			BaseUserService userService = (BaseUserService) factory.getBean(this.USER_SERVICE);
			BaseInfoService infoService = (BaseInfoService) factory.getBean(this.INFO_SERVICE);
			//获得所有要转发的信息
			BaseList infoList = infoService.getAllInfoById(temp, 3);
			//遍历每个转发的用户
			for(int i = 0; i < acceptUserIdList.length; i++){
				BaseObject acceptObject;
				if( acceptUserIdList[i].equals(name) ){
					acceptObject = sendObject;
				}else{
					acceptObject = userService.getBeanInfoById(acceptUserIdList[i]);
				}
				
				if( acceptObject == null ){
					throw new Exception();
				}
				
				for(int j = 0 ;j < infoList.size();j++){
					BaseObject infoBean = (BaseObject) factory.getBean(BaseBean.BEAN_OBJECT);
					infoBean.set(InfoConstant.INFO_SEND_USER_ID, name);
					infoBean.set(InfoConstant.INFO_SEND_USER_NAME, sendObject.get(UserConstant.USER_NAME));
					
					infoBean.set(InfoConstant.INFO_ACCEPT_USER_ID, acceptObject.get(UserConstant.USER_ID));
					infoBean.set(InfoConstant.INFO_ACCEPT_USER_NAME, acceptObject.get(UserConstant.USER_NAME));
					infoBean.set(InfoConstant.INFO_STATUS, "未读");
					infoBean.set(InfoConstant.INFO_TIME, time.getNowTime());
					
					infoBean.set(InfoConstant.INFO_NAME, infoList.get(j).get(InfoConstant.INFO_NAME));
					infoBean.set(InfoConstant.INFO_BODY, infoList.get(j).get(InfoConstant.INFO_BODY));
					
					infoBean.set(InfoConstant.INFO_USER_NUMBER, 2);
					
					
					if( infoService.insertInfo(infoBean) ){
						String infoId = (String) infoBean.get(InfoConstant.INFO_ID);
						List sendList = (List) sendObject.get(UserConstant.USER_SEND_INFO);
						if( sendList == null ){
							sendList = new ArrayList();
						}
						sendList.add(infoId);
						sendObject.set(UserConstant.USER_SEND_INFO, sendList);
						userService.updateUserBean(name, sendObject);
						
						List acceptList = (List) acceptObject.get(UserConstant.USER_ACCEPT_INFO);
						if( acceptList == null ){
							acceptList = new ArrayList();
						}
						acceptList.add(infoId);
						acceptObject.set(UserConstant.USER_ACCEPT_INFO, acceptList);
						userService.updateUserBean((String) acceptObject.get(UserConstant.USER_ID), acceptObject);
					}
				}
			}
					
			
			log.info(name + "转发信息成功");
			this.out("success");
		}catch(Exception e){
			log.info(name + "转发信息失败");
			e.printStackTrace();
			log.info(e.getMessage());
		}
	}


	public String[] getInfoIdList() {
		return infoIdList;
	}


	public void setInfoIdList(String[] infoIdList) {
		this.infoIdList = infoIdList;
	}


	public String[] getAcceptUserIdList() {
		return acceptUserIdList;
	}


	public void setAcceptUserIdList(String[] acceptUserIdList) {
		this.acceptUserIdList = acceptUserIdList;
	}
	
	
}
